
import { AnchorLiveInfoEntity } from '../entity/info';
import { DemoUserInfoEntity } from '../../demo/entity/info';
import { Init, App, Provide, Inject } from '@midwayjs/decorator';
import { BaseService, CoolTransaction } from '@cool-midway/core';
import { InjectEntityModel } from '@midwayjs/typeorm';
import { Repository, QueryRunner } from "typeorm";
import { IMidwayKoaApplication } from '@midwayjs/koa';

// 获取直播数据
const liveTotal = (anchorId) => {
  let sql = `select SUM(duration) AS total_amount1,SUM(soundWaves) AS total_amount2,SUM(gifts) AS total_amount3 from anchor_live_info where anchorId = ${anchorId}`
  return sql
}

// 获取订单总数
const orderTotal = (anchorId) => {
  let sql = `select count(*) AS total_order from order_info where anchorId = ${anchorId}`
  return sql
}

// 更新主播数据
const updateAnchor = (total_amount1, total_amount2, total_amount3, total_order, anchorId) => {
  let sql = `update demo_user_info set liveDuration = ${total_amount1},soundWave = ${total_amount2},giftCount = ${total_amount3},orderCount = ${total_order} where id = ${Number(anchorId)}`
  return sql
}

/**
 * anchorLive模块-直播信息服务
 */
@Provide()
export class AnchorLiveInfoService extends BaseService {

  @App()
  app: IMidwayKoaApplication;

  @InjectEntityModel(AnchorLiveInfoEntity)
  anchorLiveInfoEntity: Repository<AnchorLiveInfoEntity>;

  @InjectEntityModel(DemoUserInfoEntity)
  demoUserInfoEntity: Repository<DemoUserInfoEntity>;

  @Inject()
  ctx;

  @Init()
  async init() {
    await super.init();
    this.setEntity(this.anchorLiveInfoEntity);
  }

  @CoolTransaction({
    connectionName: 'default',
  })
  async add(param, queryRunner?: QueryRunner) {
    let anchorItem = await this.demoUserInfoEntity.findBy({ id: Number(param.anchorId) })
    let name = anchorItem[0].name
    let anchorName = anchorItem[0].nickname
    param.anchorName = anchorName
    param.name = name
    await super.add(param);
    let sql = liveTotal(param.anchorId)
    let res = await this.nativeQuery(sql)
    let sqlOrder = orderTotal(param.anchorId)
    let resOrder = await this.nativeQuery(sqlOrder)
    let item = res[0]
    let orderCount = resOrder[0]
    let u = updateAnchor(item.total_amount1, item.total_amount2, item.total_amount3, orderCount.total_order, param.anchorId)
    await this.nativeQuery(u)
    return {
      id: param.id,
    };
  }

  async delete(param, queryRunner?: QueryRunner) {
    let data = await this.anchorLiveInfoEntity.findBy({ id: param[0] });
    let paramItem = data[0]
    await super.delete(param);
    let sql = liveTotal(paramItem.anchorId)
    let res = await this.nativeQuery(sql)
    let sqlOrder = orderTotal(paramItem.anchorId)
    let resOrder = await this.nativeQuery(sqlOrder)
    let item = res[0]
    let orderCount = resOrder[0]
    if (!item.total_amount1) {
      item.total_amount1 = 0
      item.total_amount2 = 0
      item.total_amount3 = 0
    }
    if (!orderCount.total_order) {
      orderCount.total_order = 0
    }
    console.log(item, orderCount, 'orderCount')
    let u = updateAnchor(item.total_amount1, item.total_amount2, item.total_amount3, orderCount.total_order, paramItem.anchorId)
    await this.nativeQuery(u)
    return param
  }

  async update(param, queryRunner?: QueryRunner) {
    await super.update(param);
    let sql = liveTotal(param.anchorId)
    let res = await this.nativeQuery(sql)
    let sqlOrder = orderTotal(param.anchorId)
    let resOrder = await this.nativeQuery(sqlOrder)
    let item = res[0]
    let orderCount = resOrder[0]
    let u = updateAnchor(item.total_amount1, item.total_amount2, item.total_amount3, orderCount.total_order, param.anchorId)
    await this.nativeQuery(u)
    return param
  }

}
